package com.sanploy.fed.push.service.impl;


import com.sanploy.fed.push.bean.GovNews;
import com.sanploy.fed.push.bean.NewsClass;
import com.sanploy.fed.push.dao.GovNewsDao;
import com.sanploy.fed.push.dao.NewsClassDao;
import com.sanploy.fed.push.service.NewsService;
import com.sanploy.fed.push.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsClassDao newsClassDao;

    @Autowired
    private GovNewsDao govNewsDao;

    @Override
    public List<NewsClass> getClassByParent(Long parentId) {
        return newsClassDao.findByParentId(parentId);
    }

    @Override
    public void addNewsClass(String name, Long parentId) throws Exception {
        NewsClass newsClass = new NewsClass();
        newsClass.setParentid(parentId);
        newsClass.setName(name);
        List<NewsClass> newsClasses = newsClassDao.findByNameAndParent(parentId,name);
        if (newsClasses.size()>0){
            throw new Exception();
        }
        newsClassDao.save(newsClass);
    }

    @Override
    public List<NewsClass> findAllNewClass() {
        return (List<NewsClass>) newsClassDao.findAll();
    }

    @Override
    public void addNews(String name, String content, String author, String url, String classid,String introduce,String classname) {
        GovNews govNews = new GovNews();
        govNews.setUrl(url);
        govNews.setAddtime(DateUtil.getTimeNormal());
        govNews.setAuthor(author);
        govNews.setClassid(Long.decode(classid));
        govNews.setClassname(classname);
        govNews.setIntroduce(introduce);
        govNews.setContent(content);
        govNews.setTitle(name);
        govNewsDao.save(govNews);
    }

    @Override
    public GovNews getNewsById(Long id) {

        return govNewsDao.findOne(id);
    }

    @Override
    public List<GovNews> getNewsByClassLimit(Long newsClass,int count,int page) {
        int start = (page-1)*count;
        int end = count;
        List<GovNews> list = govNewsDao.findByClassLimit(newsClass, start,end);
        for (GovNews govNews:list){
            govNews.setContent("");
        }
        return list;
    }

    @Override
    public List<GovNews> getNewsByPage(int count, int page) {
        int start = (page-1)*count;
        int end = count;
        List<GovNews> list = govNewsDao.findByPage(start,end);
        for (GovNews govNews:list){
            govNews.setContent("");
        }
        return list;
    }

    @Override
    public List<GovNews> getNewsByPageAll(int count, int page) {
        int start = (page-1)*count;
        int end = count;
        List<GovNews> list = govNewsDao.findByPageAll(start,end);
        for (GovNews govNews:list){
            govNews.setContent("");
        }
        return list;

    }

    @Override
    public List<GovNews> getclassOneNew(Long id,int count, int page) {

        List<NewsClass> newList = getClassByParent(id);
        List<GovNews> list = new ArrayList<>();
        newList.forEach(item->{
            list.addAll(govNewsDao.findNewsByClassid(item.getId()));
        });
        int start = (page-1)*count;
        int end = start+count;

        if (start>list.size()){
            return new ArrayList<>();
        }
        List<GovNews> gList = new ArrayList<>();
        if (end>list.size()){
            for (int i = start; i < list.size(); i++) {
                gList.add(list.get(i));
            }
        }else {
            for (int i = start; i < end; i++) {
                gList.add(list.get(i));
            }
        }

        return gList;
    }

    @Override
    public List<GovNews> searchNews(String key) {

        return govNewsDao.searchTitle("%"+key+"%");
    }

    @Override
    public Long getNewsNumber() {
        return govNewsDao.count();
    }

    @Override
    public Long getNewsNumber(Long id) {
        return govNewsDao.findByPageAll(id);
    }

    @Override
    public Long getOneNewsNumber(Long id) {

        List<NewsClass> newList = getClassByParent(id);
        Long sums = 0L;
        for (NewsClass newsClass:newList){
            sums+=govNewsDao.findByPageAll(newsClass.getId());
        }
        return sums;
    }

    @Override
    public void deleteNews(Long id) {
        govNewsDao.delete(id);
    }

    @Override
    public void deleteNewsClass(Long id) {
        newsClassDao.delete(id);
    }

    @Override
    public void updateNewsClass(String name,Long id) {

        newsClassDao.setNameById(name, id);
    }

}
